package com.neusoft.nep.common;

import com.neusoft.nep.grid.entity.Fankui;
import com.neusoft.nep.grid.entity.User;
import com.neusoft.nep.common.FileUtils;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class MainViewController {
    @FXML
    private ImageView imageView;
    @FXML
    BorderPane borderPane;
    @FXML
    private StackPane stackPane;

    @FXML
    public void initialize() {
        Image image = new Image("image/welcome.png");
        imageView.setImage(image);
        //让图片和窗口自动调整大小
        imageView.fitWidthProperty().bind(borderPane.widthProperty());
        imageView.fitHeightProperty().bind(borderPane.heightProperty());
        imageView.setPreserveRatio(false);
        imageView.setSmooth(true);

    }

    public void loadView(String fxmlFilePath) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource(fxmlFilePath);
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        stackPane.getChildren().setAll(root);
    }

    public void loadgongzhongView() throws IOException {
        loadView("../supervise/view/UserLoginView.fxml");

    }

    public void loadwanggeView() throws IOException {
        User u1 = new User("1", "1");
        User u2 = new User("2", "2");
        User u3 = new User("3", "3");
        // 从文件中读取用户Map
        Map<String, User> userMap = FileUtils.readMapObject("Usergrid.txt");
        // 如果文件不存在或为空，初始化三个已知用户
        if (userMap == null || userMap.isEmpty()) {
            userMap.put(u1.getLogincode(), u1);
            userMap.put(u2.getLogincode(), u2);
            userMap.put(u3.getLogincode(), u3);
            FileUtils.writeMapObject("Usergrid.txt", userMap);
        }
        if (userMap == null || userMap.isEmpty()) {
            userMap.put(u1.getLogincode(), u1);
            userMap.put(u2.getLogincode(), u2);
            userMap.put(u3.getLogincode(), u3);
            FileUtils.writeMapObject("Usergrid.txt", userMap);
        }


        Fankui fankui1 = new Fankui("1", "康丁友", "优", "辽宁省", "沈阳市", "2025.6.20", "");
        Fankui fankui2 = new Fankui("2", "康丁友", "轻度污染", "山西省", "太原市", "2024.6.20", "");
        Fankui fankui3 = new Fankui("3", "李博", "重度污染", "湖南省", "长沙市", "2024.3.24", "");

        // 将 Fankui 对象放入 Map 中
        Map<String, Fankui> fankuiMap = new HashMap<>();
        fankuiMap.put(fankui1.getCode(), fankui1);
        fankuiMap.put(fankui2.getCode(), fankui2);
        fankuiMap.put(fankui3.getCode(), fankui3);

        // 保存到新文件（添加文件检查逻辑）
        File file = new File("Fankui.txt");
        if (!file.exists() || file.length() == 0) {
            FileUtils.writeMapObject("Fankui.txt", fankuiMap);
            System.out.println("文件保存成功");
        } else {
            System.out.println("文件已存在且不为空，取消保存");
        }


        loadView("../grid/view/login.fxml");
}
    public void loadxitongView() throws IOException {
        loadView("../admin/view/AdminLoginView.fxml");
    }
}
